use "$LocalData\BartikAnalyticData_2digit.dta", clear

* Set panel variables
xtset geography year

*Generating year-industry-shares for computation for Bartik analytics and SEs
		keep if year>2002  //Restricts to period of analysis
		
		levelsof year, local(y) //Creates a macro list of all years
		
		foreach year in `y' {
			foreach ind_var of varlist c_i_share_2000* {
				qui gen t`year'_`ind_var' = `ind_var' * (year == `year')
				}
			}
	
		qui tab year, gen(year_) //Generates year dummies
		drop year_1
		qui tab geography, gen(geography_) //Generates year dummies
		drop geography_1
		

/******************************************************************************/
/******************************************************************************/
/* Regressions                                                                */
/******************************************************************************/
/******************************************************************************/

local years  (year>2002) & abs(gsmall_dep1)<5 & gSmallFirm1!=.
*local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000
local controls 	dlngdp unem_change pop_growth annualchange 

gen testconditions = cond(`years',1,0)
bysort geography: egen insample = sum(test)
keep if insample>1






****************************************************************************************************

*Regressions of medium banks, large banks, and HHI

*Deposits
noi display "Deposit regressions."
local years  (year>2002) & abs(gmed_dep1)<5  & !missing(bartikGS1) & !missing(gSmallFirm1) & !missing(gsmall_dep1) 

	
local years  (year>2002) & abs(gmed_dep1)<5  & !missing(bartikGS1) & !missing(gSmallFirm1) & !missing(gsmall_dep1) 
eststo: xtivreg2 gmed_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
gen mainsample = e(sample)


local years mainsample==1
eststo: areg gSmallFirm1 bartikGS1 year_* if `years', vce(cluster cluster3) absorb(geography)

*Opening file to collect standard errors from Adao et al 2019
capture file close fh
file open fh  using "$LocalData\Tables\Adao_Standard_Errors_MedLgRFS.tex", write replace
file write fh "\toprule" _n
file write fh  " (1) & (2)  & (3)  & (4)  & (5) & (6) " _n


*(1)





eststo: areg gmed_dep1 gSmallFirm1 year_* if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_MedLargeRFS", tex replace ///
	ctitle(Small Dep Gr) keep(gSmallFirm1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	
*(2)
eststo: areg gmed_dep1 bartikGS1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_MedLargeRFS", tex append ///
	ctitle(Small Dep Gr) keep(bartikGS1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

	preserve

		keep if `years'
		local controls2 year_* geography_* 
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao gmed_dep1 bartikGS1 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm1)

		local se2 = string(se0[1,1], "%9.2f")
		local ciL2 = string(ciL[1,1], "%9.2f")
		local ciH2 = string(ciH[1,1], "%9.2f")
		
		local se3 =string(se_alpha[1,1], "%9.2f")
		local ciL3 =string(ciL2SLS[1,1], "%9.2f")
		local ciH3 =string(ciH2SLS[1,1], "%9.2f")

	restore	
	
local years  (year>2002) & abs(gmed_dep1)<5  & !missing(bartikGS1) & !missing(gSmallFirm1) & !missing(gsmall_dep1)  
	
*(3)
eststo: xtivreg2 gmed_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_MedLargeRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)



	
local years  (year>2002) & abs(gbig_dep1)<5  & !missing(bartikGS1) & !missing(gSmallFirm1)
xtivreg2 gbig_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(state)

drop mainsample
eststo: xtivreg2 gmed_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
gen mainsample = e(sample)
	
local years mainsample==1


	
*(4)
eststo: areg gbig_dep1 gSmallFirm1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_MedLargeRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)


	*(5)
eststo: areg gbig_dep1 bartikGS1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_MedLargeRFS", tex append ///
	ctitle(Small Dep Gr) keep(bartikGS1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)


	preserve

		keep if `years'
		local controls2 year_* geography_* 
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao gbig_dep1 bartikGS1 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm1)

		local se5 = string(se0[1,1], "%9.2f")
		local ciL5 = string(ciL[1,1], "%9.2f")
		local ciH5 = string(ciH[1,1], "%9.2f")
		
		local se6 =string(se_alpha[1,1], "%9.2f")
		local ciL6 =string(ciL2SLS[1,1], "%9.2f")
		local ciH6 =string(ciH2SLS[1,1], "%9.2f")

	restore	

*(6)
eststo: xtivreg2 gbig_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_MedLargeRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)




file write fh  " & `se1' & `se2' & `se3' & `se4' & `se5' & `se6'    \\" _n
file write fh  " & [`ciL1', `ciH1'] & [`ciL2', `ciH2'] & [`ciL3', `ciH3'] & [`ciL4', `ciH4'] & [`ciL5', `ciH5'] & [`ciL6', `ciH6']   \\" _n
	file close fh
